package org.zhxy.zhxy.controller.backend;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.zhxy.zhxy.entity.Classroom;
import org.zhxy.zhxy.entity.dto.PageDto;
import org.zhxy.zhxy.service.ClassroomService;
import org.zhxy.zhxy.utils.Result;

import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/api/admin/classroom")
public class AdminClassroomController {

    @Autowired
    private ClassroomService classroomService;

    /**
     * 分页查询
     */
    @GetMapping("/list")
    public Result listClassroom(PageDto pageDto) {
        return classroomService.listClassroom(pageDto);
    }

    /**
     * 添加教室
     */
    @PostMapping("/add")
    public Result addClassroom(@RequestBody Classroom classroom) {
        String name = classroom.getName();
        QueryWrapper<Classroom> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", name);
        Classroom one = classroomService.getOne(queryWrapper);
        if (one != null) {
            return Result.fail("该教室已存在");
        }
        Date now = new Date();
        classroom.setCreateTime(now);
        classroom.setUpdateTime(now);
        return classroomService.save(classroom) ? Result.success("添加成功") : Result.fail("添加失败");
    }

    /**
     * 删除教室
     */
    @DeleteMapping("/delete")
    public Result deleteClassroom(Integer id) {
        return classroomService.removeById(id) ? Result.success("删除成功") : Result.fail("删除失败");
    }

    /**
     * 编辑教室
     */
    @PostMapping("/update")
    public Result updateNotice(@RequestBody Classroom classroom) {
        classroom.setUpdateTime(new Date());
        return classroomService.updateById(classroom) ? Result.success("修改成功") : Result.fail("修改失败");
    }

    /**
     * 批量删除教室
     */
    @DeleteMapping("/deleteBatch")
    public Result deleteBatchClassroom(@RequestBody List<Integer> ids){
        return classroomService.removeByIds(ids) ? Result.success("批量删除教室成功") : Result.fail("批量删除教室失败");
    }

}
